package com.woniu.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.exception.order.OrderException;
import com.woniu.exception.order.OrderExceptionCode;
import com.woniu.model.Result;
import com.woniu.order.client.EmployeeClient;
import com.woniu.order.client.FlowClient;
import com.woniu.order.client.UserClient;
import com.woniu.order.cliententity.entity.Employee;
import com.woniu.order.cliententity.entity.FlowHandle;
import com.woniu.order.cliententity.form.AddHandelForm;
import com.woniu.order.mapper.ChangeDetailsMapper;
import com.woniu.order.mapper.TOrderMapper;
import com.woniu.order.model.ChangeDetails;
import com.woniu.order.model.TOrder;
import com.woniu.order.param.AddEmployeePassParam;
import com.woniu.order.param.AddTransferParam;
import com.woniu.order.param.CheckChangeByStationIdParam;
import com.woniu.order.service.ChangeDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class ChangeDetailsServiceImpl extends ServiceImpl<ChangeDetailsMapper, ChangeDetails> implements ChangeDetailsService {

    @Resource
    private TOrderMapper orderMapper;

    @Resource
    private ChangeDetailsMapper changeDetailsMapper;

    @Resource
    private EmployeeClient employeeClient;

    @Resource
    private FlowClient flowClient;


    @Override
    public ChangeDetails emplyeeTransfers(Integer userId, AddTransferParam toBean) {
        TOrder tOrder = orderMapper.selectById(toBean.getOrderNum());
        if (tOrder.getEmployId()!=userId.longValue()){
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_NO_ORDER);
        }
        ChangeDetails changeDetails = new ChangeDetails(null, toBean.getOrderNum(), userId.longValue(), null, (long)0, null, null, LocalDateTime.now(), tOrder.getStationId(), null, toBean.getReason(), null);
        //新增转单申请
        changeDetailsMapper.insert(changeDetails);
        //订单流程和状态修改
        AddHandelForm addHandelForm = new AddHandelForm(null, 13, userId, tOrder.getId(), null);
        Result<FlowHandle> add = flowClient.add(addHandelForm);
        if (add.getCode()!=200){
            throw new OrderException(add.getCode(),add.getMessage());
        }
//        tOrder.setState((long)13);
//        orderMapper.updateById(tOrder);
        return changeDetails;
    }

    @Override
    public ChangeDetails employeePass(Integer userId, AddEmployeePassParam toBean) {
        //查询登录账号
        Result<Employee> employeeResult = employeeClient.byId(userId);
        if (employeeResult.getCode()!=200){
            throw new OrderException(employeeResult.getCode(),employeeResult.getMessage());
        }
        //判断是否是站长
        if (employeeResult.getData().getRoleId()!=3){
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_NO_ORDER);
        }
        //查询转单申请
        ChangeDetails changeDetails = changeDetailsMapper.selectById(toBean.getId());
        if (employeeResult.getData().getStationId()!=changeDetails.getStationId()||changeDetails==null){
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_NO_ORDER);
        }
        //查询订单
        TOrder tOrder = orderMapper.selectById(changeDetails.getOrderNum());
        if (tOrder==null){
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_NO_ORDER);
        }
        //修改转单信息并通过
        changeDetails.setTargetId(toBean.getEmployeeId());
        changeDetails.setComment(toBean.getComment());
        changeDetails.setConfirmTime(LocalDateTime.now());
        changeDetails.setAdminId(userId.longValue());
        changeDetails.setState((long)1);
        changeDetailsMapper.updateById(changeDetails);
        //订单流程和状态修改
        AddHandelForm addHandelForm = new AddHandelForm(null, tOrder.getState().intValue(), Math.toIntExact(toBean.getEmployeeId()), tOrder.getId(), null);
        Result<FlowHandle> add = flowClient.add(addHandelForm);
        if (add.getCode()!=200){
            throw new OrderException(add.getCode(),add.getMessage());
        }
        tOrder.setEmployId(toBean.getEmployeeId());
        orderMapper.updateById(tOrder);

        return changeDetails;
    }

    @Override
    public Page<ChangeDetails> checkChangeByStationId(Integer userId, CheckChangeByStationIdParam param) {
       Integer pageNum = param.getPageNum()==null||param.getPageNum()==0?1:param.getPageNum();
       Integer pageSize = param.getPageSize()==null||param.getPageSize()==0?3:param.getPageSize();
        Page<ChangeDetails> page = new Page<>(pageNum,pageSize);
        Page<ChangeDetails> page1 = changeDetailsMapper.selectPage(page, new QueryWrapper<ChangeDetails>().eq("station_id", param.getStationId()));
        return page1;
    }

    @Override
    public Page<ChangeDetails> checkUnHandelChangeByStationId(Integer userId, CheckChangeByStationIdParam param) {
        Integer pageNum = param.getPageNum()==null||param.getPageNum()==0?1:param.getPageNum();
        Integer pageSize = param.getPageSize()==null||param.getPageSize()==0?3:param.getPageSize();
        Page<ChangeDetails> page = new Page<>(pageNum,pageSize);
        Page<ChangeDetails> page1 = changeDetailsMapper.selectPage(page, new QueryWrapper<ChangeDetails>().eq("station_id", param.getStationId()).eq("state",0));
        return page1;
    }
}
